package org.darkforest.admin.core;

import org.darkforest.common.services.GeneralService;
import org.darkforest.common.services.SettingService;
import org.darkforest.common.util.AliOssFileUploadUtil;
import org.darkforest.common.util.SystemConstant;
import org.darkforest.dao.dao.GeneralDao;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.listener.SpringContextHolder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

@SuppressWarnings("rawtypes")
public class ServerDataContext {
	public final static String DAO_NAME = "dao" ;
	public final static String SERVICE_NAME = "service" ;
	public final static String USER_SESSION_NAME = "user" ;
	public final static String MEMBER_SESSION_NAME = "memInfo" ;
	public final static String BUSINESS_SESSION_NAME = "business" ;
	public final static String CUSTOM_SESSION_NAME = "custom" ;
	public static final String PLUGIN_TYPE_TASK = "task" ;
	public static String SAVE_FILE_DIR = "/WEB-INF/data/file" ;
	public static String SERVER_ADDRESS ="platform.server.address";
	public static String DATA_DIR = "/WEB-INF/data";
	private static String dateformat = "yyyy-MM-dd HH:mm:ss" ;
	public static String REAL_PATH;
	public static Map<String , Class> resourceMap = new HashMap() ;
	private static WebApplicationContext wac;
	private static ApplicationContext context;
	private static Map<String,String> taskMsgMap = new ConcurrentHashMap<String,String>();
	private static GeneralService service ;
	private static GeneralDao dao ;
	public static DataSource datasource;
	private SettingService settingService;

	public enum ChanelTypeEnum {
		SMS,VIDEO,MUSIC,READ,GAME,FMM,ANIME;
		public String toString() {
			return super.toString().toLowerCase();
		}
	}
	public enum OperatorTypeEnum {
		CMCC,GSM,CDMA;
		public String toString() {
			return super.toString().toLowerCase();
		}
	}

	/**
	 * 初始化数据 , 在WebApplicationDataListener 初始化时调用
	 */
	public  void init(ServletContext servletContext){
		ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
		datasource=(DataSource)context.getBean("dataSource");
		settingService=(SettingService)context.getBean("settingServiceImpl");
		this.context = context;
		SpringContextHolder.getContextHolder().setContext(context);
		//设置图片路径
		AliOssFileUploadUtil.bucketName = settingService.getSettingInfo(SettingNameEnum.ALIOSS_BUCKETNAME);
		AliOssFileUploadUtil.accessId = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_ID);
		AliOssFileUploadUtil.accessKey = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_KEY);
//		AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ENDPOINT);
		AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.IMAGE_UPLOAD_ENDPOINT);
//		SystemConstant.ALIOSS_PATH = "http://" + AliOssFileUploadUtil.bucketName + ".oss-cn-beijing.aliyuncs.com";
		SystemConstant.ALIOSS_PATH = settingService.getSettingInfo(SettingNameEnum.IMAGE_SHOW_ENDPOINT);
		SystemConstant.PCWEB_PATH = settingService.getSettingInfo(SettingNameEnum.DOMAIN_NAME);
		servletContext.setAttribute("basePath",servletContext.getContextPath());
		servletContext.setAttribute("alioss_path", SystemConstant.ALIOSS_PATH);
//		SystemConstant.MEMBER_LEVEL_ZERO = Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_ZERO));
//		SystemConstant.MEMBER_LEVEL_ONE =Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_ONE));
//		SystemConstant.MEMBER_LEVEL_TWO=Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_TWO));
//		SystemConstant.MEMBER_LEVEL_THREE=Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_THREE));
//		SystemConstant.MEMBER_LEVEL_FOUR=Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_FOUR));
//		SystemConstant.MEMBER_LEVEL_FIVE=Integer.valueOf(settingService.getSettingInfo(SettingNameEnum.MEMBER_LEVEL_FIVE));

	}
	/**
	 *
	 * @param resource
	 * @return
	 */
	public static Class getResource(String resource){
		return resourceMap.get(resource) ;
	}
	/**
	 *
	 * @param clazz
	 * @return
	 */
	public static Logger getLogger(Class clazz){
		return Logger.getLogger(clazz.toString()) ;
	}
	/**
	 *
	 * @return
	 */

	public static void setWac(WebApplicationContext wac) {
		ServerDataContext.wac = wac;
	}


	public static WebApplicationContext getWac(){
		return wac ;
	}



	public static String getDateformat() {
		// TODO Auto-generated method stub
		return dateformat;
	}

	public static GeneralService getService() {
		return service==null ? (GeneralService)wac.getBean(SERVICE_NAME) : service;
	}

	public static void setService(GeneralService service) {
		ServerDataContext.service = service;
	}

	public static GeneralDao getDao() {
		return dao==null ? (GeneralDao)wac.getBean(DAO_NAME) : dao;
	}

	public static void setDao(GeneralDao dao) {
		ServerDataContext.dao = dao;
	}

	public static ApplicationContext getContext() {
		return context;
	}

	public static void setContext(ApplicationContext context) {
		ServerDataContext.context = context;
	}

	public static String getTaskMsg(String taskid){
		if(taskMsgMap.containsKey(taskid)){
			return taskMsgMap.get(taskid);
		}
		return null;
	}


	public static int setTaskMsg(String taskid,String msg){
		if(taskid !=null && msg !=null){
			if(taskMsgMap.containsKey(taskid)){
				taskMsgMap.remove(taskid);
			}
			taskMsgMap.put(taskid, msg);
			return taskMsgMap.get(taskid) !=null ? 1 :0;
		}
		return 0;
	}
	public static DataSource getDatasource() {
		return datasource;
	}


}
